package com.hemaapp.yjnh.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.view.singlewheel.LoopView;

import java.util.ArrayList;

/**
 * 类描述：滚轮选择dialog
 * 创建人：GuiyouZhang
 * 创建时间：2016/6/23 10:45
 * 修改备注：
 */
public class WheelSelectorDialog {

    private Context context;
    private Display display;
    private Dialog dialog;
    ArrayList<String> datas = new ArrayList<>();
    private onSelectedItemListener listener = null;

    private TextView tvCancel;
    private TextView tvOK;
    private LoopView loopView;

    public WheelSelectorDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public void setDatas(ArrayList<String> datas) {
        this.datas.clear();
        this.datas.addAll(datas);
    }

    public WheelSelectorDialog builder() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_single_wheel_list, null);
        view.setMinimumWidth(display.getWidth());
        dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        dialog.setContentView(view);
        //初始化dialog中的控件
        tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        tvOK = (TextView) view.findViewById(R.id.tv_ok);
        loopView = (LoopView) view.findViewById(R.id.loopview);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    int index = loopView.getSelectedItem();
                    if (index >= 0 && index < datas.size()) {
                        listener.onSelected(index);
                    }
                }
                dismiss();
            }
        });
        //设置是否循环播放
        loopView.setNotLoop();
        //设置原始数据
        loopView.setItems(datas);
        //设置初始位置
        loopView.setInitPosition(datas.size() / 2);
        //设置字体大小
        loopView.setTextSize(20);

        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);
        return this;
    }

    public void show() {
        dialog.show();
    }

    public void dismiss() {
        dialog.dismiss();
    }

    public void setListener(onSelectedItemListener listener){
        this.listener = listener;
    }
    public interface onSelectedItemListener{
        public void onSelected(int index);
    }
}
